package com.qf.fmall2203.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.fmall2203.user.entity.Users;
import com.qf.fmall2203.user.mapper.UsersMapper;
import com.qf.fmall2203.user.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fmall2203.util.GetSalt;
import com.qf.fmall2203.vo.ResultVo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户  服务实现类
 * </p>
 *
 * @author dongjl
 * @since 2023-02-13
 */
@Service

public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVo regist(Users users) {


        QueryWrapper<Users> usersQueryWrapper = new QueryWrapper<Users>().eq("username",users.getUsername());

        Users user = this.getOne(usersQueryWrapper);

        if (user!=null){

        return ResultVo.error(-1,"用户名已存在");

        }

        String salt = GetSalt.getSalt(8);

        users.setSalt(salt);

        Md5Hash md5Hash = new Md5Hash(users.getPassword(), salt, 1024);

        users.setPassword(md5Hash.toHex());

        users.setUserRegtime(new Date());

        users.setUserModtime(new Date());

        this.save(users);


        return ResultVo.ok();
    }

    @Override
    public ResultVo login(String username, String password) {


        return null;
    }
}
